﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Data.Entity;
using DBModelConnection;
using System.IO;
using System.Reflection;

namespace WcfBLService
{
    public class Service1 : IService1
    {
        private ModelWorker dbModel = new ModelWorker();

        public void SaveVideoToServiceEnvironment(VideoClass video)
        {
            //string defaultFolder = System.AppDomain.CurrentDomain.BaseDirectory;
            //string navigateToFolder = "..\\..\\AppData\\Videos\\" + video.name + ".wmv";
            //string sourceDir = Path.Combine(defaultFolder, navigateToFolder);

            //FileStream fs = new FileStream(sourceDir, FileMode.OpenOrCreate, FileAccess.ReadWrite);
            //fs.Write(video.videoByte, 0, video.videoByte.Length);
            //fs.Close();

            //navigateToFolder = "..\\..\\AppData\\Thunmbnails\\" + video.name + ".jpeg";
            //sourceDir = Path.Combine(defaultFolder, navigateToFolder);

            //fs = new FileStream(sourceDir, FileMode.OpenOrCreate, FileAccess.ReadWrite);
            //fs.Write(video.samllImageBuffer, 0, video.samllImageBuffer.Length);
            //fs.Close();

            //C:\Users\mayaOri\sela\mediaPlayerProj\תיקיה חדשה\WcfBLService\AppData\Videos\E-Fest.wmv
            //C:\Users\mayaOri\sela\mediaPlayerProj\תיקיה חדשה\WcfBLService\AppData\Videos\Elie Gets Wet.wmv
            //C:\Users\mayaOri\sela\mediaPlayerProj\תיקיה חדשה\WcfBLService\AppData\Videos\free Style.wmv
            //C:\Users\mayaOri\sela\mediaPlayerProj\תיקיה חדשה\WcfBLService\AppData\Videos\Ladies.wmv
            //C:\Users\mayaOri\sela\mediaPlayerProj\תיקיה חדשה\WcfBLService\AppData\Videos\Joe Nall.wmv

            //C:\Users\mayaOri\sela\mediaPlayerProj\תיקיה חדשה\WcfBLService\AppData\Thunmbnails\E-Fest.jpeg
            //C:\Users\mayaOri\sela\mediaPlayerProj\תיקיה חדשה\WcfBLService\AppData\Thunmbnails\Elie Gets Wet.jpeg
            //C:\Users\mayaOri\sela\mediaPlayerProj\תיקיה חדשה\WcfBLService\AppData\Thunmbnails\free Style.jpeg
            //C:\Users\mayaOri\sela\mediaPlayerProj\תיקיה חדשה\WcfBLService\AppData\Thunmbnails\Ladies.jpeg
            //C:\Users\mayaOri\sela\mediaPlayerProj\תיקיה חדשה\WcfBLService\AppData\Thunmbnails\Joe Nall.jpeg

            //C:\Users\mayaOri\sela\mediaPlayerProj\תיקיה חדשה\WcfBLService\AppData\BImages\Facebook.jpeg
            //C:\Users\mayaOri\sela\mediaPlayerProj\תיקיה חדשה\WcfBLService\AppData\BImages\elalSmall.jpeg
            //C:\Users\mayaOri\sela\mediaPlayerProj\תיקיה חדשה\WcfBLService\AppData\BImages\elalPlain.jpeg
            //C:\Users\mayaOri\sela\mediaPlayerProj\תיקיה חדשה\WcfBLService\AppData\BImages\elalLogo.jpeg
            //C:\Users\mayaOri\sela\mediaPlayerProj\תיקיה חדשה\WcfBLService\AppData\BImages\Blackberry.jpeg
            //C:\Users\mayaOri\sela\mediaPlayerProj\תיקיה חדשה\WcfBLService\AppData\BImages\androidVSiPhone.jpeg
            //C:\Users\mayaOri\sela\mediaPlayerProj\תיקיה חדשה\WcfBLService\AppData\BImages\Android.jpeg
            //C:\Users\mayaOri\sela\mediaPlayerProj\תיקיה חדשה\WcfBLService\AppData\BImages\youtube.jpeg
            //C:\Users\mayaOri\sela\mediaPlayerProj\תיקיה חדשה\WcfBLService\AppData\BImages\motorola.jpeg
            //C:\Users\mayaOri\sela\mediaPlayerProj\תיקיה חדשה\WcfBLService\AppData\BImages\Microsoft.jpeg
            //C:\Users\mayaOri\sela\mediaPlayerProj\תיקיה חדשה\WcfBLService\AppData\BImages\Google-t.jpeg
            //C:\Users\mayaOri\sela\mediaPlayerProj\תיקיה חדשה\WcfBLService\AppData\BImages\google-logo.jpeg
            //C:\Users\mayaOri\sela\mediaPlayerProj\תיקיה חדשה\WcfBLService\AppData\BImages\Gmail.jpeg
            //C:\Users\mayaOri\sela\mediaPlayerProj\תיקיה חדשה\WcfBLService\AppData\BImages\facebook-Israel.jpeg
        }

        public MyVideoClass GetVideoById(int video_id)
        {
            var video = dbModel.getSpecipicVideo(video_id);
            var banners = GetAllBannersForVudeo(video_id);
            if (video != null)
            {
                //FileStream fileStream = new FileStream(video.VideoUrl, FileMode.Open, FileAccess.Read);
                //BinaryReader binaryReader = new BinaryReader(fileStream);
                //int numOfBytes = ((int)(new FileInfo(video.VideoUrl).Length));
                //byte[] videoBuffer = binaryReader.ReadBytes(numOfBytes);
                //fileStream.Close();
                //binaryReader.Close();

                return new MyVideoClass() { videoUrl = video.VideoUrl, banners = banners };
            }
            else
                return null;
        }

        public bool SaveVideoToDB(VideoClass video)
        {
            Video dbVideo = new Video(); 
            //{ Name = video.name, video_binary = video.videoByte, video_pic = video.smallImageByte };
            return dbModel.AddVideoToDB(dbVideo);
        }

        public bool DeleteVideoById(int id)
        {
            return dbModel.DeleteVideoById(id);
        }

        public bool UpdateVideoById(int id, string name, byte[] videoStream, byte[] imageStream)
        {
            return dbModel.UpdateVideoById(id, name, videoStream, imageStream);
        }

        public ListViewClass GetAllThumbnailVideoAvailable() 
        {
            var list = dbModel.GetAllSmallImageVideos();
            ListViewClass thumbnails = new ListViewClass();
            thumbnails.ListViewItems = new List<ThumbnailClass>();
            var length = list.Count;
            for (int i = 0; i < length; i++)
            {
                //FileStream fileStream = new FileStream(list[i].ThunmbnailUrl, FileMode.Open, FileAccess.Read);
                //BinaryReader binaryReader = new BinaryReader(fileStream);
                //int numOfBytes = ((int)(new FileInfo(list[i].ThunmbnailUrl).Length));
                //byte[] imageBuffer = binaryReader.ReadBytes(numOfBytes);
                //fileStream.Close();
                //binaryReader.Close();

                thumbnails.ListViewItems.Add(new ThumbnailClass() 
                { id = list[i].Id, imageUrl = list[i].ThunmbnailUrl, name = list[i].Name });
            }
            return thumbnails;
        }

        public List<MyBannerClass> GetAllBannersForVudeo(int id)
        {
            //var banners = new List<BannerClass>();
            var bannerArr = new List<MyBannerClass>();
            var tmpBanners = dbModel.GetAllBannerForVideo(id);

            foreach (var item in tmpBanners)
            {
                //var b = new BannerClass();
                var br = new MyBannerClass();

                if (File.Exists(item.Key.BannerUrl))
                {
                    br.bannerUrl = item.Key.BannerUrl;
                    br.bannerSize = (int)item.Value.Key.Size;
                    br.bannerTime = (int)item.Value.Value.Time;
                    bannerArr.Add(br);
                    // Changing the fileStream to string URL...

                    //FileStream fileStream = new FileStream(item.Key.BannerUrl, FileMode.Open, FileAccess.Read);
                    //BinaryReader binaryReader = new BinaryReader(fileStream);
                    //int numOfBytes = ((int)(new FileInfo(item.Key.BannerUrl).Length));
                    //byte[] imageBuffer = binaryReader.ReadBytes(numOfBytes);
                    //fileStream.Close();
                    //binaryReader.Close();

                    //b.bannerContent = imageBuffer;
                    //b.bannerSize = (int)item.Value.Key.Size;
                    //b.bannerTime = (int)item.Value.Value.Time;
                    //banners.Add(b);
                }
            }
            return bannerArr;
            //return banners;
        }
    }
}
